package com.cobra.activiti.utils;

import org.springframework.util.StringUtils;

/**
 * @author yaobin
 * @date 2020/4/21 0021 17:15
 * @Description 时间工具方法
 */
public class DateUtil {


    public static String getTimeDiffStr(Long diff) {

        if (diff == null) {
            return null;
        }

        if (0 == diff) {
            return "0毫秒";
        }

        // 计算
        StringBuilder strBuilder=new StringBuilder();

        int ss = 1000;
        int mi = ss * 60;
        int hh = mi * 60;
        int dd = hh * 24;

        long day = diff / dd;
        long hour = (diff - day * dd) / hh;
        long minute = (diff - day * dd - hour * hh) / mi;
        long second = (diff - day * dd - hour * hh - minute * mi) / ss;
        long milliSecond = diff - day * dd - hour * hh - minute * mi - second * ss;

        //天
        String strDay = day < 10 ? "0" + day : "" + day;
        //小时
        String strHour = hour < 10 ? "0" + hour : "" + hour;
        //分钟
        String strMinute = minute < 10 ? "0" + minute : "" + minute;
        //秒
        String strSecond = second < 10 ? "0" + second : "" + second;

        //毫秒
        String strMilliSecond = milliSecond < 10 ? "0" + milliSecond : "" + milliSecond;
        strMilliSecond = milliSecond < 100 ? "0" + strMilliSecond : "" + strMilliSecond;

        if (!strDay.equals("00")) {
            strBuilder.append(strDay).append("天").
            append(strHour).append("时").
            append(strMinute).append("分").
            append(strSecond).append("秒").
            append(strMilliSecond).append("毫秒");
        }

        if (strDay.equals("00") && !strHour.equals("00")) {
            strBuilder.append(strHour).append("时").
                    append(strMinute).append("分").
                    append(strSecond).append("秒").
                    append(strMilliSecond).append("毫秒");
        }
        if (strDay.equals("00") && strHour.equals("00") && !strMinute.equals("00")) {
            strBuilder.append(strMinute).append("分").
                    append(strSecond).append("秒").
                    append(strMilliSecond).append("毫秒");
        }
        if (strDay.equals("00") && strHour.equals("00") && strMinute.equals("00")&&!strSecond.equals("00")) {
            strBuilder.append(strSecond).append("秒").
                    append(strMilliSecond).append("毫秒");
        }

        return strBuilder.toString();
    }

}
